package com.example.hotelapp.Activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.icu.util.Calendar;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.hotelapp.Adapter.DetailAdapter;
import com.example.hotelapp.Adapter.OrderAdapter;
import com.example.hotelapp.Base.BaseActivity;
import com.example.hotelapp.Bean.BookInfoBean;
import com.example.hotelapp.Bean.RoomDetailBean;
import com.example.hotelapp.R;
import com.example.hotelapp.Utils.AppNetConfig;
import com.example.hotelapp.Utils.MyCommonUtils;
import com.example.utils.CommonUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

import static com.example.hotelapp.Utils.MyCommonUtils.*;
import static com.example.utils.CommonUtils.*;

/**
 * 详细页
 */
public class DetailActivity extends BaseActivity {

//    找到view对象，View注入框架（添加至第三方开源框架）
    @BindView(R.id.iv_jdtupian)
    ImageView ivJdtupian;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvpj1)
    TextView tvpj1;
    @BindView(R.id.tvpj2)
    TextView tvpj2;
    @BindView(R.id.into)
    TextView into;
    @BindView(R.id.btn_total)
    TextView btnTotal;
    @BindView(R.id.leave)
    TextView leave;
    @BindView(R.id.ll_show)
    RecyclerView llShow;
    private Activity activity;
    private DetailAdapter detailAdapter;
    private int startday,endday;
    private int total;


    /**
     * 加载布局
     */
    @Override
    protected int setLayoutId() {
        return R.layout.activity_detail;
    }

    /**
     * 初始化
     */
    @Override
    protected void init() {
        activity = this;
        setSystemBarColor(activity, R.color.white);
        String id = getIntent().getStringExtra("id");
        String names = getIntent().getStringExtra("name");
        String imgs = getIntent().getStringExtra("img");
        Glide.with(this).load(AppNetConfig.HotelImgUrl + imgs).into(ivJdtupian);
        tvTitle.setText(names);
        InitView(id);
    }

    /**
     * 获取传递过来的参数
     */
    public static void start(Context context, String id,String name,String img) {
        Intent intent = new Intent(context, DetailActivity.class);
        intent.putExtra("id", id);
        intent.putExtra("name", name);
        intent.putExtra("img", img);
        context.startActivity(intent);
    }

    /**
     * 选择时间
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private void SelectTime(TextView timemark,int mark) {
        Calendar mcalendar = Calendar.getInstance(Locale.CHINA);
        int year = mcalendar.get(Calendar.YEAR);         //  得到当前年
        int month = mcalendar.get(Calendar.MONTH);       //  得到当前月
        int day = mcalendar.get(Calendar.DAY_OF_MONTH);  //  得到当前日

        new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {      //  日期选择对话框
            @SuppressLint("SetTextI18n")
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                int month2 = month + 1; //因为当前会减一
                String m = String.valueOf(month2);
                String d = String.valueOf(dayOfMonth);
                if (month2 < 10){
                    m = "0" + month2;
                }
                if (dayOfMonth < 10){
                    d = "0" + dayOfMonth;
                }
                if (mark == 1){
                    startday = dayOfMonth;
                    timemark.setText(m + "月" + d + "日");
                }else if (mark == 2){
                    endday = dayOfMonth;
                    total = endday-startday;
                    btnTotal.setText("共" + total + "晚");
                    timemark.setText(m + "月" + d + "日");
                }
            }
        }, year, month, day).show();   //  弹出日历对话框时，默认显示 年，月，日
    }

    /**
     * 加载数据
     */
    private void InitView(String id) {
        OKPostValue("hotelId", id, AppNetConfig.SelectroomInfoView, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastShow(activity,"服务器出错，请稍后再试");
                    }
                });
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //获取返回的json数据并解析
                String json = response.body().string();
                System.out.println(json);
                RoomDetailBean databean = new Gson().fromJson(json, RoomDetailBean.class);
                int code = databean.getCode();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (code == 1) {
                            //请求成功
                            llShow.setLayoutManager(new GridLayoutManager(activity, 1));
                            detailAdapter = new DetailAdapter(activity, databean.getData());
                            llShow.setAdapter(detailAdapter);
                            detailAdapter.setItemClickListener(new DetailAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(RecyclerView.ViewHolder viewHolder, String typename, String bedname, Double price,int id, int position) {
                                    String start = into.getText().toString().trim();
                                    String end = leave.getText().toString().trim();
                                    if (start.equals("入住时间") || end.equals("离开时间")){
                                        ToastShow(activity,"请选择时间");
                                    }else {
                                        String totalPrice = String.valueOf(price * total);
                                        BookActivity.start(activity, String.valueOf(id),typename,bedname,start,end,String.valueOf(total),totalPrice,tvTitle.getText().toString());
                                    }
                                }
                            });
                        } else {
                            ToastShow(activity,"空空如也");
                            llShow.setAdapter(null);
                        }
                    }
                });
            }
        });
    }

    /**
     * 点击事件
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.into, R.id.leave})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.into:
                SelectTime(into,1);
                break;
            case R.id.leave:
                if (into.getText().toString().equals("入住时间")){
                    ToastShow(activity,"请先选择入住时间");
                    return;
                }
                SelectTime(leave,2);
                break;
        }
    }
}
